const express = require('express');
const router = express.Router();
const { Course } = require('../models');
const { success, failure } = require('../utils/responses');
const { Op } = require('sequelize');
router.get("/", async function (req, res) {
    try {
        const query = req.query;
        const page = query.page || 1;
        const size = query.size || 10;
     
        const offset = (page - 1) * size;

        const condition = {
            attributes: { exclude: ['CategoryId', 'UserId', 'content'] },
            order: [['id', 'DESC']],
            limit: size,
            offset: offset
        };
        if (query.name) {
            condition.where = { name: { [Op.like]: `%${query.name}%` } }
        }
        const { count, rows } = await Course.findAndCountAll(condition)
        success(res, '查询课程成功。', { courses: rows, pagination: { total: count, page, size } });

    } catch (error) {
        failure(res, error);
    }
})
module.exports = router;
